package com.tongji.housediy.view.component3D
{
	import alternativa.engine3d.core.Camera3D;
	
	import flash.geom.Point;
	
	public class CameraControl
	{
		
		public function CameraControl()
		{
		}
		
		public static function collisionDetect(camera:Camera3D, direction:Point, outsidePosition2D:Vector.<Number>, insidePosition2D:Vector.<Number>):Boolean
		{
			var start:Point = new Point();
			var end:Point = new Point();
			var moveable:Boolean = true; 
			
			//外墙碰撞检测
			for (var i:int = 0;i<outsidePosition2D.length / 4;i++)
			{
				start.x = outsidePosition2D[4 * i];
				start.y = outsidePosition2D[4 * i + 1];
				end.x = outsidePosition2D[4 * i + 2];
				end.y = outsidePosition2D[4 * i + 3];
				if (isIntersect(start, end, new Point(direction.x, direction.y), new Point(camera.x, camera.y)))
					moveable = false;
			}
			//内墙碰撞检测
			for (var j:int = 0;j<insidePosition2D.length / 4;j++)
			{
				start.x = insidePosition2D[4 * j];
				start.y = insidePosition2D[4 * j + 1];
				end.x = insidePosition2D[4 * j + 2];
				end.y = insidePosition2D[4 * j + 3];
				if (isIntersect(start, end, new Point(direction.x, direction.y), new Point(camera.x, camera.y)))
					moveable = false;
			}
			
			return moveable;
			
		}
		
		private static function isSame(startPoint:Point, endPoint:Point, pPoint:Point, qPoint:Point):int
		{
			var dx:Number = endPoint.x - startPoint.x;  
			var dy:Number = endPoint.y - startPoint.y;  
			var dx1:Number = pPoint.x - startPoint.x;  
			var dy1:Number = pPoint.y - startPoint.y;  
			var dx2:Number = qPoint.x - endPoint.x;  
			var dy2:Number = qPoint.y - endPoint.y;  
			return ((dx * dy1 - dy * dx1) * (dx * dy2 - dy * dx2) > 0 ? 1 : 0);  
		}
		
		private static function isIntersect(startPoint:Point, endPoint:Point, pPoint:Point, qPoint:Point):int
		{
			return (isSame(startPoint, endPoint, pPoint, qPoint)==0 && 
				　　isSame(pPoint, qPoint, startPoint, endPoint)==0) ? 1 : 0;
		}
		
	}
}